package com.tanhua.server.controller;

import com.tanhua.domain.vo.*;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户信息控制层
 */
@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户信息
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity queryUserInfo(Long userID, Long huanxinID) {
        UserInfoVo userInfoVo = userInfoService.queryUserInfo(userID, huanxinID);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户信息
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity updateUserInfo(@RequestBody UserInfoVo userInfoVo) {
        userInfoService.updateUserInfo(userInfoVo);
        return ResponseEntity.ok(null);
    }

    /**
     * 用户通用设置 - 查询
     */
    @RequestMapping(value = "/settings", method = RequestMethod.GET)
    public ResponseEntity querySettings() {
        SettingsVo settingsVo = userInfoService.querySettings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 用户通用设置 - 修改
     */
    @RequestMapping(value = "/notifications/setting", method = RequestMethod.POST)
    public ResponseEntity updateSettings(@RequestBody Map<String, Boolean> params) {
        Boolean likeNotification = params.get("likeNotification");
        Boolean pinglunNotification = params.get("pinglunNotification");
        Boolean gonggaoNotification = params.get("gonggaoNotification");
        userInfoService.updateSettings(likeNotification,
                pinglunNotification,
                gonggaoNotification);
        return ResponseEntity.ok(null);
    }


    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     */
    @RequestMapping(value = "/counts", method = RequestMethod.GET)
    public ResponseEntity queryCounts() {
        CountsVo countsVo = userInfoService.queryCounts();
        return ResponseEntity.ok(countsVo);
    }


    /**
     * 互相喜欢，喜欢，粉丝、谁看过我 - 分页查询
     * 1 互相关注
     * 2 我关注
     * 3 粉丝
     * 4 谁看过我
     */
    @RequestMapping(value = "/friends/{type}", method = RequestMethod.GET)
    public ResponseEntity queryFriendsByPage(@PathVariable("type") int type,@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pagesize) {
        PageResult<FriendVo> pageResult = userInfoService.queryFriendsByPage(type,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 粉丝-喜欢
     */
    @RequestMapping(value = "/fans/{uid}", method = RequestMethod.POST)
    public ResponseEntity makeFriends(@PathVariable("uid") Long friendId) {
        userInfoService.makeFriends(friendId);
        return ResponseEntity.ok(null);
    }
}
